loader-utils
Methods
getLoaderConfig
Recommended way to retrieve the loader config:
config = loaderUtils.getLoaderConfig(this, "myLoader");
Tries to read the loader config from the webpack.config.js
under the given property name ("myLoader"
in this case) and merges the result with the loader query. For example, if your webpack.config.js
had this property...
cheesecakeLoader: {
type: "delicious",
slices: 4
}
...and your loader was called with ?slices=8
, getLoaderConfig(this, "cheesecakeLoader")
would return
{
type: "delicious",
slices: 8
}
It is recommended that you use the camelCased loader name as your default config property name.
parseQuery
var query = loaderUtils.parseQuery(this.query);
assert(typeof query == "object");
if(query.flag)
null -> {}
? -> {}
?flag -> { flag: true }
?+flag -> { flag: true }
?-flag -> { flag: false }
?xyz=test -> { xyz: "test" }
?xyz[]=a -> { xyz: ["a"] }
?flag1&flag2 -> { flag1: true, flag2: true }
?+flag1,-flag2 -> { flag1: true, flag2: false }
?xyz[]=a,xyz[]=b -> { xyz: ["a", "b"] }
?a%2C%26b=c%2C%26d -> { "a,&b": "c,&d" }
?{json:5,data:{a:1}} -> { json: 5, data: { a: 1 } }
stringifyRequest
Turns a request into a string that can be used inside require()
or import
while avoiding absolute paths.
Use it instead of JSON.stringify(...)
if you're generating code inside a loader.
Why is this necessary? Since webpack calculates the hash before module paths are translated into module ids, we must avoid absolute paths to ensure
consistent hashes across different compilations.
This function:
- resolves absolute requests into relative requests if the request and the module are on the same hard drive
- replaces
\
with /
if the request and the module are on the same hard drive - won't change the path at all if the request and the module are on different hard drives
- applies
JSON.stringify
to the result
loaderUtils.stringifyRequest(this, "./test.js");
loaderUtils.stringifyRequest(this, ".\\test.js");
loaderUtils.stringifyRequest(this, "test");
loaderUtils.stringifyRequest(this, "test/lib/index.js");
loaderUtils.stringifyRequest(this, "otherLoader?andConfig!test?someConfig");
loaderUtils.stringifyRequest(this, require.resolve("test"));
loaderUtils.stringifyRequest(this, "C:\\module\\test.js");
loaderUtils.stringifyRequest(this, "C:\\module\\test.js");
loaderUtils.stringifyRequest(this, "\\\\network-drive\\test.js");
urlToRequest
Converts some resource URL to a webpack module request.
var url = "path/to/module.js";
var request = loaderUtils.urlToRequest(url);
Module URLs
Any URL containing a ~
will be interpreted as a module request. Anything after the ~
will be considered the request path.
var url = "~path/to/module.js";
var request = loaderUtils.urlToRequest(url);
Root-relative URLs
URLs that are root-relative (start with /
) can be resolved relative to some arbitrary path by using the root
parameter:
var url = "/path/to/module.js";
var root = "./root";
var request = loaderUtils.urlToRequest(url, root);
To convert a root-relative URL into a module URL, specify a root
value that starts with ~
:
var url = "/path/to/module.js";
var root = "~";
var request = loaderUtils.urlToRequest(url, root);
interpolateName
Interpolates a filename template using multiple placeholders and/or a regular expression.
The template and regular expression are set as query params called name
and regExp
on the current loader's context.
var interpolatedName = loaderUtils.interpolateName(loaderContext, name, options);
The following tokens are replaced in the name
parameter:
[ext]
the extension of the resource[name]
the basename of the resource[path]
the path of the resource relative to the context
query parameter or option.[folder]
the folder of the resource is in.[emoji]
a random emoji representation of options.content
[emoji:<length>]
same as above, but with a customizable number of emojis[hash]
the hash of options.content
(Buffer) (by default it's the hex digest of the md5 hash)[<hashType>:hash:<digestType>:<length>]
optionally one can configure
- other
hashType
s, i. e. sha1
, md5
, sha256
, sha512
- other
digestType
s, i. e. hex
, base26
, base32
, base36
, base49
, base52
, base58
, base62
, base64
- and
length
the length in chars
[N]
the N-th match obtained from matching the current file name against options.regExp
Examples
loaderUtils.interpolateName(loaderContext, "js/[hash].script.[ext]", { content: ... });
loaderUtils.interpolateName(loaderContext, "html-[hash:6].html", { content: ... });
loaderUtils.interpolateName(loaderContext, "[hash]", { content: ... });
loaderUtils.interpolateName(loaderContext, "[emoji]", { content: ... });
loaderUtils.interpolateName(loaderContext, "[emoji:4]", { content: ... });
loaderUtils.interpolateName(loaderContext, "[sha512:hash:base64:7].[ext]", { content: ... });
loaderUtils.interpolateName(loaderContext, "picture.png");
loaderUtils.interpolateName(loaderContext, "[path][name].[ext]?[hash]", { content: ... });
loaderUtils.interpolateName(loaderContext, "script-[1].[ext]", { regExp: "page-(.*)\\.js", content: ... });
getHashDigest
var digestString = loaderUtils.getHashDigest(buffer, hashType, digestType, maxLength);
buffer
the content that should be hashedhashType
one of sha1
, md5
, sha256
, sha512
or any other node.js supported hash typedigestType
one of hex
, base26
, base32
, base36
, base49
, base52
, base58
, base62
, base64
maxLength
the maximum length in chars
License
MIT (http://www.opensource.org/licenses/mit-license.php)